exports.TouchHandler = Core.MessageListener.subclass(
{
	classname: 'TouchHandler',
    initialize: function(layer) 
    {
		this._touchTarget = null;
		this._parentLayer = null;
		this._multiArray = null;
		this._panningOffset = null;
		
		this._parentLayer = layer;
		this._multiArray = new Array();
    },
	
	handleTouches: function() {
		this._touchTarget = new GL2.TouchTarget();
		//this._target.setAnchor(0.5,0.5);
		//this._touchTarget.setSize(1,1);
		this._touchTarget.setSize(TCUtil.getScreenWidth(),TCUtil.getAdjustedHeight(this._parentLayer.getNodeHeight()));
		this._touchTarget.getTouchEmitter().addListener(this, this.onTouch,10);
		this._touchTarget.setDepth(100);
		//this._touchTarget.setColor(1.0, 0.5, 1.0);
		this._parentLayer.addChild(this._touchTarget);
	},
	
	updateTouchTarget:function(position, size) {
		if(position) 
		{
			this._touchTarget.setPosition(position.getX(), position.getY());
		}
		this._touchTarget.setSize(TCUtil.getScreenWidth(),TCUtil.getAdjustedHeight(this._parentLayer.getNodeHeight()));
		if(size) 
		{
			//console.log("Zulu: In Size ");
			this._touchTarget.setSize(size[0], size[1]);
		}
	},
	
    touchBegan: function(touch)
    {
//													 console.log("Zulu: Touch Began");
        //this.myTouchBegan(touch);
        
        this._multiArray[this._multiArray.length] = touch;
        
        if(this._multiArray.length == 1)// save this one for offset
        {
            var panningOffset = new Core.Vector();
            panningOffset.setX(this._parentLayer.getPosition().getX() - this._multiArray[0].getPosition().getX());
            panningOffset.setY(this._parentLayer.getPosition().getY() - this._multiArray[0].getPosition().getY());
			
            this._panningOffset = panningOffset;
        }

    },
    
    myTouchMoved: function(touch,multiArrayLength)
    {
        
    },
    
    touchMoved: function(touch)
    {

													 //console.log("Zulu: TouchHandler  Moved");
		this.myTouchMoved(touch,this._multiArray.length);

        var i , touch1 = null;
		
        for(i = 0; i<this._multiArray.length; i++)

        {
            touch1 = this._multiArray[i];
            if(touch1.getId() == touch.getId())
            {   
                this._multiArray[i] = touch;
            }
        }
        this.multiTouch(this._multiArray);
    },
    
    myTouchEnded: function(touch)
    {
        
    },
    
    touchEnded: function(touch,myTouchArrayLength)
    {
        //this.myTouchEnded(touch,this._multiArray.length);
        var newArray = new Array() , i;
        for( i = 0; i<this._multiArray.length ; i++)
        {
            if(this._multiArray[i].getId() != touch.getId())
            {
                newArray[newArray.length] = this._multiArray[i];
            }
        }
        this._multiArray = newArray;
        if(this._multiArray.length == 0)//reset everything
        {
            this._diffForPinch = 0;
            this._lastDistance = 0;
        }
													
		this._isPinched = false;
		this._isPanned = false;										
    },
    
	onTouch: function(touch)
	{

        
        switch(touch.getAction())
		{
			case touch.Action.Start:
				
                this.touchBegan(touch);
                return true;
                break;
            
            case touch.Action.End:
                this.touchEnded(touch);
				break;
			
            
            case touch.Action.Move:
				this.touchMoved(touch);
				break;
		}
        //
	},
    
    multiTouch: function(fullArray)
    {
        if(fullArray.length == 2) // it's a pinch
        {
			return;
            if(this._isPinchingDisabled)
            {
                return;
            }
            this._isPanningDisabled = true;
			this._isPinched = true;
							
            var touch1 , touch2 , distance , location1 , location2, diff, currentScale , scaleFactor;
            
            touch1 = fullArray[0];
            touch2 = fullArray[1];

            distance = 0;
            
            location1 = touch1.getPosition();
            location2 = touch2.getPosition();
            
            distance = Math.sqrt( Math.pow(location1.getX()-location2.getX(),2) +  Math.pow(location1.getY()-location2.getY(),2));
            
            diff = this._lastDistance - distance;

            this._lastDistance = distance;
            
            this._diffForPinch += diff;

            if(Math.abs(this._diffForPinch) >2)
            {
                currentScale = this._parentLayer.getScale();
                scaleFactor = .02;

                if(this._diffForPinch <0)
                {
                    

                    if(currentScale.getX() <= 1)
                    {
                            this._parentLayer.setScale(currentScale.getX() + scaleFactor,currentScale.getY()+scaleFactor);
                    }
                }
                else
                {
                    if(currentScale.getX() >= (0.4 - this._ratio/50))
                    {
                        this._parentLayer.setScale(currentScale.getX() -scaleFactor,currentScale.getY()-scaleFactor);
                        //NgLogE("zooming down for X "+this._parentLayer.getScale().getX());
                    }
                }
                this._diffForPinch = 0;
                
                var offsetAdjustedVector , currScale;
                offsetAdjustedVector = this._parentLayer.getPosition();
                
                currScale = this._parentLayer.getScale().getX();
	            if(offsetAdjustedVector.getX() > (this._width/2.0)*currScale)
	            {
	                offsetAdjustedVector.setX((this._width/2.0)*currScale);
	                //NgLogE("limit");
	            }
	            else if(offsetAdjustedVector.getX() + ((this._width/2)*currScale) <  Core.Capabilities.getScreenHeight())
	                offsetAdjustedVector.setX(Core.Capabilities.getScreenHeight() - (this._width/2)*currScale);
	            
	            if(offsetAdjustedVector.getY() > (this._height/2.0)*currScale)
	            {
	                offsetAdjustedVector.setY((this._height/2.0)*currScale);
	                //NgLogE("limit");
	            }
	            else if(offsetAdjustedVector.getY() + ((this._height/2)*currScale) < Core.Capabilities.getScreenWidth())
	                offsetAdjustedVector.setY(Core.Capabilities.getScreenWidth() - (this._height/2)*currScale);
	            
	            var sampleVect , result;
	            
	            sampleVect = new Core.Vector(this._parentLayer.getPosition().getX() + ((this._width/2.0)*currScale),this._parentLayer.getPosition().getY() + ((this._height/2.0)*currScale));
	            
	            result = this._parentLayer.getParent().localToScreen(sampleVect);
	            
	            this._parentLayer.setPosition(offsetAdjustedVector);
            }
            
        }
        else if(fullArray.length == 1) // it's panning
        {
            if(this._isPanningDisabled)
            {
                return;
            }
													
			//this._isPanned = true;
            
            var offsetAdjustedVector , currScale , sampleVect , result;
            
            offsetAdjustedVector = new Core.Vector();
            offsetAdjustedVector.setY(this._multiArray[0].getPosition().getY() + this._panningOffset.getY());
            offsetAdjustedVector.setX(this._parentLayer.getParent().getCenterPositionOfMallForZoomOut().getX());
            	
			var tempPosition = offsetAdjustedVector;
			var mallContainer = MMGameLayer.getMallContainer();
			var mallContainerHeight = mallContainer.getSize().getY();
			var scaleY = mallContainer.getScale().getY();
			var mallHeight = TCUtil.getAdjustedHeight(this._parentLayer.getNodeHeight());
			var dummyFloorHeight = 0;
			var actionAlertWindowHeight = 0;
			if(this._parentLayer.getDummyFloor())
			{
				dummyFloorHeight = TCUtil.getAdjustedHeight(FLOOR_CONSTRUCTION_HEIGHT);
			}
			
			if(MMGameLayer.getControlLayer().getActionAlertWindow())
			{
				//console.log("zzz:: alert is "+MMGameLayer.getControlLayer().getActionAlertWindow());
				actionAlertWindowHeight = TCUtil.getAdjustedHeight(ACTION_ALERT_WINDOW_HEIGHT);
			}
		
			if((tempPosition.getY() < 0)  || ((((mallHeight+dummyFloorHeight)*(scaleY)) + actionAlertWindowHeight - offsetAdjustedVector.getY())) < mallContainerHeight)
			{
				// Yeh hum UPPER nahi JANAY de rahay || nechay nahi aanay da rahay
			} 
			
 			else 
 			{
				this._parentLayer.setPosition(offsetAdjustedVector);
			}			
        }
    },

	destroy: function()
	{
		
	},
   
});
